﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tong.XmlHttp;
using System.Text.RegularExpressions;
public partial class aspnet_admin_bock_collect_CollectExe_testContPage : System.Web.UI.Page
{
    Tong.TongUse TextData = new Tong.TongUse();
    GetWebData webDate = new GetWebData();
    public string pageURL = "";//页面地址
    public string RegexStr1 = ""; //正则
    public string pagecode = "";
    string Scont = "";
    string Linkurl = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ListPage"] != null)
        {

            Scont = Session["ListPage"].ToString().ToLower();
            pagecode = Request.QueryString["pagecode"].Trim();
            pageURL = Request.QueryString["pageURL"].Trim();
            RegexStr1 = Request.QueryString["RegexStr"].Trim();
            urlx.Text = pageURL;
            try
            {
                string getforntx;//得到前页面的东东
                string getlastx;//得到正则后面的东东
                string RegexStr = "";
                string baseurl = urlx.Text.Trim();//base url 得到数据的URL
                MatchCollection ATag;///A标签
               // TextBox1.Text = Scont;
                ATag = Regex.Matches(Scont, @"<(\/){0,1}a[^<>]*>");
                // Session["ListPage"] = Scont;

                for (int i = 0; i < ATag.Count; i++)
                {
                    
                    if (ATag[i].ToString().ToLower().IndexOf("href") != -1)
                    {
                        //Scont = Scont.Replace(ATag[i].ToString(), Regex.Replace(ATag[i].ToString(), RegexStrxContPage(".*"), "[链接到内容页]"));

                        //得到link
                        RegexStr = RegexStr1.Replace("[链接到内容页]", "╊");
                        getforntx = RegexStr.Split('╊')[0];//得到前页面的东东
                        getlastx = RegexStr.Split('╊')[1];//得到正则后面的东东

                        //开始得到正则搜索到的数据 
                        RegexStr = Regex.Match(ATag[i].ToString(), RegexStrxContPage(RegexStr1)).Value.ToString();
                        RegexStr = RegexStr.Replace(getforntx, "").Replace(getlastx, "");//这里以经可以 准确的输出 联接页面

                        if (RegexStr.Trim() != "")
                        {
                            Linkurl = TextData.FormAturl(RegexStr, baseurl);
                            TextBox1.Text += Linkurl + "\r\n";
                            Scont = "<a href=" + Linkurl + " target=\"_blank\">" + Linkurl + "</a>";
                        }
                    }

                }

                if (Scont != "")
                {
                    Session["LinkPage"] = Linkurl;//把LINK页面传到东东里
                    tt.Text = " 您可以点此查看页面:" + Scont ;

                }
                else
                {
                    tt.Text = "没有搜索到相应内容";
                }
            }
            catch
            {
                tt.Text = "没有找到内容,请确认第一步是否有显示内容";
            }
        }

       /* //测试数据
        try
        {
            string RegexStr = "";
            pagecode = Request.QueryString["pagecode"].Trim();
            string Scont = "";
            pageURL = Request.QueryString["pageURL"].Trim();
            RegexStr1 = Request.QueryString["RegexStr"].Trim();

            RegexStr = RegexStrX(RegexStr1.Trim());
            urlx.Text = pageURL;
            Scont = webDate.GetWebDate(pageURL, pagecode);
            Scont = Regex.Match(Scont, RegexStr).Value;
            Session["ListPage"] = Scont;
            TextBox1.Text = Scont;
            if (Scont != "")
            {
                tt.Text = " 读取数据成功";

            }
            else
            {
                tt.Text = "没有搜索到相应内容";
            }
        }
        catch (Exception ex)
        {
            tt.Text = ex.Message;
        }
        */
    }










    /// <summary>
    /// 格式化得到列表内容的正 则
    /// </summary>
    /// <param name="Xstr">传入的字符</param>
    /// <returns></returns>
    public string RegexStrX(string Xstr)
    {
        return Xstr.Replace("[内容列表]", @"[\W\w]+");

    }

    /// <summary>
    /// 格式化得到列表内容的正 则
    /// </summary>
    /// <param name="Xstr">
    /// 传入正则
    /// </param>

    /// <returns></returns>
    public String RegexStrxContPage(string Xstr)
    {
        return Xstr.Replace("[链接到内容页]", @"[\r\n\s]{0,}.*");
    }



    protected void Button1_ServerClick(object sender, EventArgs e)
    {
       
    }
}
